
package GUI;

import java.awt.*;

import javax.swing.*;

import Entity.Customer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

/**
 * @author aviat
 *
 */
public class RemoveCustomer extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private JButton btnBack;
	private JTextField lNametextField;

	private JLabel lblGLastName;
	private JLabel lblBFirstName;
	private JLabel lblBLastName;
	private JLabel lblBPhone;
	private JTextField bPhogeTextField;
	private JComboBox<String> bLnamecomboBox;
	

	private JLabel iconlabel;
	private JLabel lblGFirstName;
	private JLabel lblGroomPhone;
	private JLabel lblAddress;
	private JTextField gPonetextField;
	private JButton btnRemove;
	private JComboBox<String> gFnameComboBox;
	private JComboBox<String> gLnameComboBox;
	private JComboBox<String> bFnameComboBox;
	private JButton btnRefresh;
	
	private ArrayList<Customer> c_allDate;
	private ArrayList<Customer> c_filteredDate;
	private Object syncObject;
	private boolean c_isChangeAllowed;
	private JTextField EmailtextField;
	private JComboBox<String> AddresscomboBox;

	
	public RemoveCustomer() {
		setBackground(new Color(255, 51, 0));
		setLayout(null);
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(RemoveCustomer.class.getResource("/resources/back22.png")));
		lNametextField = new JTextField();
		gPonetextField = new JTextField();
		lNametextField.setEditable(false);
		AddresscomboBox = new JComboBox<String>();
		AddresscomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						filterList(AddresscomboBox.getSelectedItem().toString(),"ADDRESS");
					}
				}
			}
		});
		gFnameComboBox = new JComboBox<String>();
		gLnameComboBox = new JComboBox<String>();
		bPhogeTextField = new JTextField();
		bPhogeTextField.setEditable(false);
		btnRemove = new JButton("Remove");
		bLnamecomboBox = new JComboBox<String>();
		bFnameComboBox = new JComboBox<String>();
		btnRefresh = new JButton("refresh");

		initialize();
	}

	private void initialize() {


		JLabel lblCustomerDetails = new JLabel("Remove Customer");
		lblCustomerDetails.setForeground(new Color(0, 204, 255));
		lblCustomerDetails.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblCustomerDetails.setBounds(10, 11, 211, 44);
		add(lblCustomerDetails);
		
		btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnBack.setBounds(10, 462, 101, 27);
		add(btnBack);


		
		JLabel labelEmail = new JLabel("Email");
		labelEmail.setForeground(Color.WHITE);
		labelEmail.setFont(new Font("Tahoma", Font.BOLD, 12));
		labelEmail.setBounds(36, 297, 66, 27);
		add(labelEmail);
		

		lblGLastName = new JLabel("Groom Last Name");
		lblGLastName.setForeground(Color.WHITE);
		lblGLastName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblGLastName.setBounds(35, 149, 111, 22);
		add(lblGLastName);
		
		lblBFirstName = new JLabel("Bride First Name");
		lblBFirstName.setForeground(Color.WHITE);
		lblBFirstName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblBFirstName.setBounds(309, 68, 111, 22);
		add(lblBFirstName);
		
		lblBLastName = new JLabel("Bride Last Name");
		lblBLastName.setForeground(Color.WHITE);
		lblBLastName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblBLastName.setBounds(313, 149, 101, 22);
		add(lblBLastName);
		
		lblBPhone = new JLabel("Bride Phone");
		lblBPhone.setForeground(Color.WHITE);
		lblBPhone.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblBPhone.setBounds(312, 228, 82, 22);
		add(lblBPhone);
		
		
		bPhogeTextField.setColumns(10);
		bPhogeTextField.setBounds(430, 227, 101, 27);
		add(bPhogeTextField);

		ImageIcon LOGOImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resources/camera-background3.png"));
		ImageIcon LOGO = new ImageIcon (LOGOImageIcon.getImage().getScaledInstance(-1, -1, java.awt.Image.SCALE_SMOOTH));
		
		lblGFirstName = new JLabel("Groom First Name");
		lblGFirstName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblGFirstName.setForeground(Color.WHITE);
		lblGFirstName.setBounds(35, 66, 127, 26);
		add(lblGFirstName);
		
		lblGroomPhone = new JLabel("Groom Phone");
		lblGroomPhone.setForeground(Color.WHITE);
		lblGroomPhone.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblGroomPhone.setBounds(35, 231, 95, 17);
		add(lblGroomPhone);
		
		lblAddress = new JLabel("Address");
		lblAddress.setForeground(Color.WHITE);
		lblAddress.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblAddress.setBounds(565, 69, 66, 21);
		add(lblAddress);
		
		
		gPonetextField.setEditable(false);
		gPonetextField.setBounds(161, 227, 101, 27);
		add(gPonetextField);
		gPonetextField.setColumns(10);
		
		
		btnRemove.setIcon(new ImageIcon(RemoveCustomer.class.getResource("/resources/delete.png")));
		btnRemove.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnRemove.setBounds(407, 406, 118, 27);
		btnRemove.setEnabled(false);
		add(btnRemove);
		
		
		gFnameComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						filterList(gFnameComboBox.getSelectedItem().toString(),"GFNAME");
					}
				}
			}
		});
		gFnameComboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		gFnameComboBox.setBounds(161, 66, 101, 27);
		add(gFnameComboBox);
		
		
		gLnameComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						filterList(gLnameComboBox.getSelectedItem().toString(),"GLNAME");
					}
				}
			}
		});
		gLnameComboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		gLnameComboBox.setBounds(161, 149, 101, 27);
		add(gLnameComboBox);
		
		
		bLnamecomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						filterList(bLnamecomboBox.getSelectedItem().toString(),"BLNAME");
					}
				}
			}
		});;
		bLnamecomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		bLnamecomboBox.setBounds(430, 149, 101, 27);
		add(bLnamecomboBox);
		
		
		bFnameComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						filterList(bFnameComboBox.getSelectedItem().toString(),"BFNAME");
					}
				}
			}
		});
		bFnameComboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		bFnameComboBox.setBounds(430, 66, 101, 27);
		add(bFnameComboBox);
		
		
		btnRefresh.setIcon(new ImageIcon(RemoveCustomer.class.getResource("/resources/arrow-refresh-2-icon.png")));
		btnRefresh.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						if(!(gPonetextField.getText().equals(""))){
							gPonetextField.setText("");
							bPhogeTextField.setText("");
							EmailtextField.setText("");
						}
						btnRemove.setEnabled(false);
						filterList(null,"ALL");
					}
				}
			}
		});
		btnRefresh.setBounds(246, 406, 118, 27);
		add(btnRefresh);
		
		EmailtextField = new JTextField();
		EmailtextField.setEditable(false);
		EmailtextField.setBounds(161, 298, 150, 27);
		add(EmailtextField);
		EmailtextField.setColumns(10);
		
		
		AddresscomboBox.setBounds(641, 70, 132, 61);
		add(AddresscomboBox);
		
		iconlabel = new JLabel("");
		iconlabel.setIcon(LOGO);
		iconlabel.setBounds(0, 0, 786, 500);
		add(iconlabel);
	}

	public void setAllCustomer(ArrayList<Customer> list){
		c_allDate = list;
		c_filteredDate = new ArrayList<>();
		syncObject = new Object();
		filterList(null,"ALL");
		 
	}
	
	private void filterList(String selectedItem, String scope)
	{
		synchronized (syncObject) {
			c_filteredDate.clear();
			
			switch(scope)
			{
			case "GFNAME":
				for (Customer c :c_allDate) 
				{
					if(c.getgFname().equals(selectedItem))
					{
						c_filteredDate.add(c);
					}
				}
				break;
			case "GLNAME":
				for (Customer c :c_allDate) 
				{
					if(c.getgLname().equals(selectedItem))
					{
						c_filteredDate.add(c);
					}
				}
				break;
			case "BFNAME":
				for (Customer c :c_allDate) 
				{
					if(c.getbFname().equals(selectedItem))
					{
						c_filteredDate.add(c);
					}
				}
				break;
			case "BLNAME":
				for (Customer c :c_allDate) 
				{
					if(c.getbLname().equals(selectedItem))
					{
						c_filteredDate.add(c);
					}
				}
				break;
			case "ADDRESS":
				for (Customer c :c_allDate) 
				{
					if(c.getAddress().equals(selectedItem))
					{
						c_filteredDate.add(c);
					}
				}
				break;
			case "ALL":
				c_filteredDate.addAll(c_allDate);
				break;
			}
		}
		c_isChangeAllowed = false;
		filterComboBox(gFnameComboBox,"GFNAME");
		filterComboBox(gLnameComboBox,"GLNAME");
		filterComboBox(bFnameComboBox,"BFNAME");
		filterComboBox(bLnamecomboBox,"BLNAME");
		filterComboBox(AddresscomboBox,"ADDRESS");
		
		if(gFnameComboBox.getItemCount()==1 && gLnameComboBox.getItemCount()==1 && bFnameComboBox.getItemCount()==1 && 
				bLnamecomboBox.getItemCount()==1 && AddresscomboBox.getItemCount()==1){
			System.out.println("mmmm");
			setCustDetails();
		}
		c_isChangeAllowed = true;

	}
	/**
	* This method adding items to combobox
	* @param combo is the combobox to add item to
	* @param scope is the key code for adding the correct field 
	* 
	*/
	private void filterComboBox(JComboBox<String> combo, String scope)
	{
		combo.removeAllItems();
		synchronized (syncObject)
		{
			for(Customer c: c_filteredDate )
			{
				System.out.println("in forrrr");
				switch(scope)
				{
				case "GFNAME":
					System.out.println(c.getgFname());
					combo.addItem(c.getgFname());
					break;
				case "GLNAME":
					System.out.println(c.getgLname());
					combo.addItem(c.getgLname());
					break;
				case "BFNAME":
					System.out.println(c.getbFname());
					combo.addItem(c.getbFname());
					break;
				case "BLNAME":
					System.out.println(c.getbLname());
					combo.addItem(c.getbLname());
					break;
				case "ADDRESS":
					combo.addItem(c.getAddress());
					break;
				}
			}
		}
	}
	/**
	* @return the e_isChangeAllowed
	* 
	*/
	private boolean isChangeAllowed()
	{
		return c_isChangeAllowed;
	}
	
	/**
	 * This method add the customers details
	 */
	public void setCustDetails(){
			
			for(Customer c : c_filteredDate){
				
				if(c.getAddress().equals((AddresscomboBox.getSelectedItem().toString())))
				{
					gPonetextField.setText(c.getgPhone());
					gPonetextField.setEditable(false);
					bPhogeTextField.setText(c.getbPhone());
					bPhogeTextField.setEditable(false);
					EmailtextField.setText(c.getEmail());
					EmailtextField.setEditable(false);
					btnRemove.setEnabled(true);
					
				}
				break;
			}
			
	}
	/**
	 * @return the gPonetextField
	 */
	public JTextField getgPonetextField() {
		return gPonetextField;
	}

	/**
	 * @param gPonetextField the gPonetextField to set
	 */
	public void setgPonetextField(JTextField gPonetextField) {
		this.gPonetextField = gPonetextField;
	}

	/**
	 * @return the emailtextField
	 */
	public JTextField getEmailtextField() {
		return EmailtextField;
	}

	/**
	 * @param emailtextField the emailtextField to set
	 */
	public void setEmailtextField(JTextField emailtextField) {
		EmailtextField = emailtextField;
	}


	/**
	 * @return the bPhogeTextField
	 */
	public JTextField getbPhogeTextField() {
		return bPhogeTextField;
	}

	/**
	 * @param bPhogeTextField the bPhogeTextField to set
	 */
	public void setbPhogeTextField(JTextField bPhogeTextField) {
		this.bPhogeTextField = bPhogeTextField;
	}

	/**
	 * @return the bLnamecomboBox
	 */
	public JComboBox<String> getbLnamecomboBox() {
		return bLnamecomboBox;
	}

	/**
	 * @return the gFnameComboBox
	 */
	public JComboBox<String> getgFnameComboBox() {
		return gFnameComboBox;
	}

	/**
	 * @return the gLnameComboBox
	 */
	public JComboBox<String> getgLnameComboBox() {
		return gLnameComboBox;
	}

	/**
	 * @return the bFnameComboBox
	 */
	public JComboBox<String> getbFnameComboBox() {
		return bFnameComboBox;
	}

	/**
	 * @return the btnBack
	 */
	public JButton getBtnBack() {
		return btnBack;
	}

	/**
	 * @return the btnRemove
	 */
	public JButton getBtnRemove() {
		return btnRemove;
	}

	/**
	 * @return the addresscomboBox
	 */
	public JComboBox<String> getAddresscomboBox() {
		return AddresscomboBox;
	}
	
}

